package shc.graduation.http.manager;

import android.os.AsyncTask;

import java.util.concurrent.Executor;

import shc.graduation.http.getter.CodeInfoGetter;
import shc.graduation.http.getter.PaoWangArticleGetter;
import shc.graduation.http.getter.PaperInfoGetter;
import shc.graduation.http.listener.TaskListener;
import shc.graduation.http.task.BaseTask;
import shc.graduation.http.task.CodeInfoTask;
import shc.graduation.http.task.PaoWangArticleTask;
import shc.graduation.http.task.PaperInfoTask;
import shc.graduation.http.task.TaskOption;


/**
 * Created by shuhuiChen on  2016/5/21  14:01.
 * Email ：shuhuiCSH@126.com
 */
public class TaskManager {

    private Executor mExecutor = AsyncTask.THREAD_POOL_EXECUTOR;
    private static TaskManager mTaskManager = null;

    private TaskManager() {

    }

    public static TaskManager getInstance() {
        if (mTaskManager == null) {
            synchronized (TaskManager.class) {
                if (mTaskManager == null) {
                    mTaskManager = new TaskManager();
                }
            }
        }
        return mTaskManager;
    }

    public void addCodeInfoTask(final TaskOption taskOption, final TaskListener taskListener) {
        if (mExecutor != null) {
            TaskRunnable taskRunnable = new TaskRunnable(taskOption, taskListener);

            CodeInfoTask codeInfoTask = new CodeInfoTask();
            codeInfoTask.setGetter(new CodeInfoGetter());
            taskRunnable.setTask(codeInfoTask);

            mExecutor.execute(taskRunnable);
        }
    }

    public void addArticleTask(TaskOption taskOption, TaskListener taskListener) {
        if (mExecutor != null) {
            TaskRunnable taskRunnable = new TaskRunnable(taskOption, taskListener);

            PaoWangArticleTask articleTask = new PaoWangArticleTask();
            articleTask.setGetter(new PaoWangArticleGetter());
            taskRunnable.setTask(articleTask);

            mExecutor.execute(taskRunnable);
        }
    }

    public void addPaperTask(TaskOption taskOption, TaskListener taskListener) {
        if (mExecutor != null) {
            TaskRunnable taskRunnable = new TaskRunnable(taskOption, taskListener);

            PaperInfoTask paperInfoTask = new PaperInfoTask();
            paperInfoTask.setGetter(new PaperInfoGetter());
            taskRunnable.setTask(paperInfoTask);

            mExecutor.execute(taskRunnable);
        }
    }


    private class TaskRunnable implements Runnable {

        private BaseTask mBaseTask = null;
        private TaskOption mTaskOption = null;
        private TaskListener mTaskListener = null;

        public TaskRunnable(TaskOption taskOption, TaskListener taskListener) {
            this.mTaskOption = taskOption;
            this.mTaskListener = taskListener;
        }

        public void setTask(BaseTask baseTask) {
            this.mBaseTask = baseTask;
        }

        @Override
        public void run() {
            if (mBaseTask != null) {
                mBaseTask.executeTask(this.mTaskOption, this.mTaskListener);
            }
        }
    }

}
